package controller

import (
	"fmt"
	"goapp-web-server/app/middleware/jwt"
	"goapp-web-server/app/models"
	"goapp-web-server/app/service"

	"github.com/kataras/iris"
)

type CmsArticleController struct {
	Service service.CmsArticleService
}

// list
func (c *CmsArticleController) Get(ctx iris.Context) {
	data, _ := c.Service.GetAll()
	ctx.JSON(iris.Map{"code": 0, "data": data})
}

// create
func (c *CmsArticleController) Post(ctx iris.Context) {
	data := jwt.GetUser(ctx)
	fmt.Println("userid", data["userid"])
	article := models.CmsArticle{}
	err := ctx.ReadJSON(&article)
	if err != nil {
		ctx.StatusCode(iris.StatusBadRequest)
		ctx.WriteString(err.Error())
	}
	fmt.Println("article:", article)
	id, err := c.Service.Add(article)
	if err != nil {
		ctx.JSON(iris.Map{"code": -1, "data": nil})
	} else {
		ctx.JSON(iris.Map{"code": 0, "data": id})
	}
}

// info
func (c *CmsArticleController) GetBy(id int64, ctx iris.Context) {
	data, _ := c.Service.GetByID(id)
	ctx.JSON(iris.Map{"code": 0, "data": data})
}

// update
func (*CmsArticleController) PutBy(id int64) string {
	return `update {id}`
}

// delete
func (*CmsArticleController) DeleteBy(id int64) string {
	return `delete {id}`
}
